<?php
namespace Admin\Model;
use Think\Model;
/**
* Modelsmodel
*/
class ModelModel extends Model
{   
    //初始化
    protected function _initialize() {
        parent::_initialize();
        $this->fieldPath = APP_PATH . 'Admin/Sql/';
    }

    /**
     * 获取可用字段类型列表
     * @return array
     */
    public function getMainTable() {
        $fields = file_get_contents ($this->fieldPath . 'xwcms_zhubiao.sql');
        return $fields;
    }
    public function getFieldList() {
        $fields = file_get_contents ($this->fieldPath . 'xwcms_insert.sql');
        return $fields;
    }

    
	/**
     * 模型导出
     * @param type $modelid 模型ID
     * @return boolean
     */
    public function export($modelid) {
        if (empty($modelid)) {
            $this->error = '请指定需要导出的模型！';
            return false;
        }
        //取得模型信息
        $info = $this->where(array('modelid' => $modelid))->find();
        if (empty($info)) {
            $this->error = '该模型不存在，无法导出！';
            return false;
        }
        unset($info['modelid']);
        //数据
        $data = array();
        $data['model'] = $info;
        //取得对应模型字段
        $fieldList = M('ModelField')->where(array('modelid' => $modelid))->select();
        if (empty($fieldList)) {
            $fieldList = array();
        }
        //去除fieldid，modelid字段内容
        foreach ($fieldList as $k => $v) {
            unset($fieldList[$k]['fieldid'], $fieldList[$k]['modelid']);
        }
        $data['field'] = $fieldList;
        return base64_encode(json_encode($data));
        // return $data;
    }

     /**
     * 模型导入
     * @param type $data 数据
     * @param type $table 导入的模型表名
     * @param type $name 模型名称
     * @return int|boolean
     */
    public function import($data, $table = '', $name = '') {
        if (empty($data)) {
            $this->error = '没有导入数据！';
            return false;
        }
        //解析
        $data = json_decode(base64_decode($data), true);
        if (empty($data)) {
            $this->error = '解析数据失败，无法进行导入！';
            return false;
        }
        //取得模型数据
        $model = $data['model'];
        if (empty($model)) {
            $this->error = '解析数据失败，无法进行导入！';
            return false;
        }
        if ($name) {
            $model['name'] = $name;
        }
        if ($table) {
            $model['table'] = $table;
        }
        //导入
        $modelid = $this->add($model);
        if ($modelid) {
            $sql="DROP TABLE IF EXISTS `xwcms_model_{$model['table']}`;
                  CREATE TABLE `xwcms_model_{$model['table']}` (
                    `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
                    `modelid` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '模型id',
                    PRIMARY KEY (`id`)
                  ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='{$model['table']}模型表';";//删除对应模型附表
            $this->execute($sql);//执行创建对应的模型表
            if (!empty($data['field'])) {
                echo "1";
                foreach ($data['field'] as $value) {
                    $value['modelid'] = $modelid;
                    if ($value['setting']) {
                        $value['setting'] = unserialize($value['setting']);
                    }
                    $fields = D('Field')->addField($value);
                    if ($fields == false) {
                        $value['setting'] = serialize($value['setting']);
                        D('Field')->where(array('modelid' => $modelid, 'field' => $value['field'], 'name' => $value['name']))->save($value);
                    }
                    unset($fields);
                }
            }
            return $modelid;
        } else {
            return false;
        }
    }
}